package com.reading.core.utils;

import com.reading.common.utils.Md5Util;
import com.reading.common.utils.StringUtil;

/**
 * @author guwenqi_1995@163.com
 * @date 2019/7/10 12:55 PM
 **/
public class PasswordUtil {

    /**
     * 获取盐
     *
     * @return
     */
    public static String randomSalt(){
        return StringUtil.getRandomString(GlobalConstant.SALT_LENGTH);
    }

    /**
     * 验证密码
     *
     * @param email     邮箱
     * @param password  密码
     * @param salt      盐
     * @param dataPwd   数据库中的加密密码
     * @return
     */
    public static Boolean matches(String email,String password,String salt,String dataPwd) {
        //加密
        String encryptPwd = encryptPassword(email, password, salt);
        return dataPwd.equals(encryptPwd);
    }

    /**
     * 密码加密
     *
     * @param email     邮箱
     * @param password  密码
     * @param salt      盐
     * @return
     */
    public static String encryptPassword(String email, String password, String salt) {
        return Md5Util.encrypt(email + password + salt, Md5Util.BitNum.Bit16);
    }

    public static void main(String[] args) {
        String password = encryptPassword("hxyggwc", "hxyggwc", "03a9cf");

        System.out.println("encryption password content : {" + password + "}");
    }
}
